package com.ruoyi.project.goods.bill.service.impl;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.common.MyConstant;
import com.ruoyi.project.goods.billHead.domain.GoodsBillHead;
import com.ruoyi.project.goods.billHead.service.IGoodsBillHeadService;
import com.ruoyi.project.repertory.domain.GoodsRepertory;
import com.ruoyi.project.repertory.service.IGoodsRepertoryService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.goods.bill.mapper.GoodsBillMapper;
import com.ruoyi.project.goods.bill.domain.GoodsBill;
import com.ruoyi.project.goods.bill.service.IGoodsBillService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 发货管理Service业务层处理
 * 
 * @author lonsn
 * @date 2020-11-04
 */
@Service
public class GoodsBillServiceImpl implements IGoodsBillService 
{
    @Autowired
    private GoodsBillMapper goodsBillMapper;

    @Autowired
    private IGoodsRepertoryService goodsRepertoryService;

    @Autowired
    private IGoodsBillHeadService goodsBillHeadService;

    /**
     * 查询发货管理
     * 
     * @param billNo 发货管理ID
     * @return 发货管理
     */
    @Override
    public GoodsBill selectGoodsBillById(String billNo)
    {
        return goodsBillMapper.selectGoodsBillById(billNo);
    }

    /**
     * 查询发货管理列表
     * 
     * @param goodsBill 发货管理
     * @return 发货管理
     */
    @Override
    public List<GoodsBill> selectGoodsBillList(GoodsBill goodsBill)
    {
        return goodsBillMapper.selectGoodsBillList(goodsBill);
    }

    /**
     * 新增发货管理
     * 
     * @param goodsBill 发货管理
     * @return 结果
     */
    @Override
    public int insertGoodsBill(GoodsBill goodsBill)
    {
        goodsBill.setBillNo(StringUtils.createBillNo());
        goodsBill.setStatus(1);//发货单默认值
        return goodsBillMapper.insertGoodsBill(goodsBill);
    }

    /**
     * 修改发货管理
     * 
     * @param goodsBill 发货管理
     * @return 结果
     */
    @Override
    public int updateGoodsBill(GoodsBill goodsBill)
    {
        return goodsBillMapper.updateGoodsBill(goodsBill);
    }

    /**
     * 删除发货管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteGoodsBillByIds(String ids)
    {
        return goodsBillMapper.deleteGoodsBillByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除发货管理信息
     * 
     * @param billNo 发货管理ID
     * @return 结果
     */
    @Override
    public int deleteGoodsBillById(String billNo)
    {
        return goodsBillMapper.deleteGoodsBillById(billNo);
    }

    /**
     * 审核发货单
     *
     * @param goodsBill 审核发货管理
     * @return 结果
     */
    @Override
    public int auditGoodsBill(GoodsBill goodsBill) {
        //更改状态
        //将发货单状态改成入库
        goodsBill.setStatus(MyConstant.BILL_STATUS_RUKU);
        goodsBillMapper.auditGoodsBill(goodsBill);

        //获取发货单明细
        GoodsBillHead goodsBillHead=new GoodsBillHead();
        goodsBillHead.setBillNo(goodsBill.getBillNo());
        List<GoodsBillHead> listBillHead= goodsBillHeadService.selectGoodsBillHeadList(goodsBillHead);
        int result=0;
        for (GoodsBillHead head : listBillHead){
            //调整库存
            result+=dealWithRepertory(head,goodsBill.getDeptId(),goodsBill.getCreateBy());
        }
        return result;
    }
    //处理库存
    private int dealWithRepertory(GoodsBillHead head, Integer deptId,String curUserName){
        int result=0;
        //判断是否有库存数据，有update，无insert
        //查询条件，deptId
        String styleNumber=head.getStyleNumber();
        GoodsRepertory repertory=goodsRepertoryService.selectGoodsRepertory(deptId,styleNumber);
        if(repertory==null){
            repertory.setCreateLoginName(curUserName);
            repertory=new GoodsRepertory();
            repertory.setDeptId(deptId);
            repertory.setStyleNumber(head.getStyleNumber());
            repertory.setQuantity(head.getQuantity());
            result=goodsRepertoryService.insertGoodsRepertory(repertory);
        }else{
            repertory.setLastUpdateLoginName(curUserName);
            repertory.setStyleNumber(head.getStyleNumber());
            repertory.setDeptId(deptId);
            repertory.setQuantity(head.getQuantity()+repertory.getQuantity());//库存相加
            result=goodsRepertoryService.updateGoodsRepertory(repertory);
        }
        return  result;
    }
}
